package top.kpromise.user.service_imp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import top.kpromise.user.data.Config;
import top.kpromise.user.email.EmailTools;
import top.kpromise.user.mapper.UserMapper;
import top.kpromise.user.model.response.UserModel;
import top.kpromise.user.service.SmsService;
import top.kpromise.user.service.UserService;
import top.kpromise.user.service.VerifyCodeService;
import top.kpromise.user.utils.RandomUtils;
import top.kpromise.user.utils.RegularUtils;

@Service
public class UserServiceImp implements UserService {

    private final UserMapper userMapper;
    private final EmailTools emailTools;
    private final SmsService smsService;
    private final VerifyCodeService verifyCodeService;

    private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());

    public UserServiceImp(UserMapper userMapper, EmailTools emailTools, SmsService smsService,
                          VerifyCodeService verifyCodeService) {
        this.userMapper = userMapper;
        this.emailTools = emailTools;
        this.smsService = smsService;
        this.verifyCodeService = verifyCodeService;
    }

    @Override
    public UserModel findUserByPhone(String phone) {
        return userMapper.findUserByPhone(phone);
    }

    @Override
    public UserModel findUserById(String id) {
        return userMapper.findUserById(id);
    }

    @Override
    public UserModel findUserByEmail(String email) {
        return userMapper.findUserByEmail(email);
    }

    @Override
    public UserModel findUserByLoginValue(String loginValue) {
        return userMapper.findUserByLoginValue(loginValue);
    }

    @Override
    public int getUserCountByPhone(String phone) {
        return userMapper.getUserCountByPhone(phone);
    }

    @Override
    public int getUserCountByEmail(String email) {
        return userMapper.getUserCountByEmail(email);
    }

    @Override
    public int insert(String id, String phone, String email) {
        return userMapper.insert(id, phone, email);
    }

    @Override
    public int getUserCountById(String id) {
        return userMapper.getUserCountById(id);
    }

    @Override
    public void updateEnableByUserId(String userId, int enabled) {
        userMapper.updateEnableByUserId(userId, enabled);
    }

    @Override
    public void updateLoginValue(String userId, String newLoginValue) {
        if (RegularUtils.isEmail(newLoginValue)) {
            userMapper.updateEmail(userId, newLoginValue);
        }else {
            userMapper.updatePhone(userId, newLoginValue);
        }
    }

    /**
     *  发送短信 或者 邮件，激活用户部分
     */

    @Override
    public void sendCreateUserCode(String loginValue) {
        if (RegularUtils.isEmail(loginValue)) {
            createUserByEmailCode(loginValue);
        }else {
            createUserByPhoneCode(loginValue);
        }
    }

    @Override
    public void sendChangeLoginValueCode(String receiver, String code) {
        logger.error("===change phone code is " + code);
        if (RegularUtils.isEmail(receiver)) {
            saveCode(receiver, code, Config.VerifyCodeType.changeLoginEmail);
        }else {
            saveCode(receiver, code, Config.VerifyCodeType.changeLoginPhone);
        }
    }

    @Override
    public void sendBindNewLoginValueCode(String loginValue, String code) {
        logger.error("===bind new loginValue, code is " + code + " and loginValue is " + loginValue);
        if (RegularUtils.isEmail(loginValue)) {
            saveCode(loginValue, code, Config.VerifyCodeType.bindNewEmail);
        }else {
            saveCode(loginValue, code, Config.VerifyCodeType.bindNewPhone);
        }
    }

    @Override
    public void sendResetPasswordCode(String loginValue, String code) {
        logger.error("===sendResetPasswordCode, code is " + code + " and loginValue is " + loginValue);
        if (RegularUtils.isEmail(loginValue)) {
            saveCode(loginValue, code, Config.VerifyCodeType.resetPassword);
        }else {
            saveCode(loginValue, code, Config.VerifyCodeType.resetPassword);
        }
    }

    private void createUserByPhoneCode(String phone) {
        String smsCode = RandomUtils.verifyCode(Config.SMS_LEN);
        saveCode(phone, smsCode, Config.VerifyCodeType.createAccount);
        smsService.createUserCode(phone, smsCode);
    }

    private void createUserByEmailCode(String email) {
        String smsCode = RandomUtils.verifyCode(Config.SMS_LEN);
        saveCode(email, smsCode, Config.VerifyCodeType.createAccount);
        emailTools.sendActiveUserVerifyCode(email, smsCode);
    }

    private void saveCode(String loginValue, String code, int type) {
        verifyCodeService.saveCode(loginValue, code, type);
    }
}
